package REMC.replicaExchange;

import static java.lang.Integer.parseInt;

import java.io.IOException;

import main.Configuration;
import main.Grid;
import main.Sequence;
import REMC.objects.CVector;
import REMC.objects.MyLogger;
import REMC.objects.Replica;
/**
 * @authors Tal & Adiel
 * this class is used to check the replica exchange algorithm.
 */
public class ReplicaExchangeMain {
	
	public static void main(String[] args) {
		
		//reading configuration file and 
		Configuration config = getConfiguration(args);
		Sequence sequence = new Sequence( config.sequence);
		Grid grid = new Grid(config.sequence.length(), config.dimensions);
		//creating one replica (to be the base of all others in the vector)
		Replica replica = new Replica(config.dimensions, sequence, 
				config.random, grid, "replica_0",new Double(0.1));
		//creating vector of replicas
		CVector cVec = new CVector(replica, config.numOfReplicas);
		
		//printing vector before replica exchange to compare later
		MyLogger.getLogger().Log("************ Before Replica Exchange ************");
		MyLogger.getLogger().Log(cVec.toStringShort());
		MyLogger.getLogger().Log("**************** Going to start replica exchange simulation ****************");
		
		//creating replica exchange object
		ReplicaExchange exchange = new ReplicaExchange(config);
		//starting main algorithm
		cVec = exchange.simulation(cVec);	
		
		MyLogger.getLogger().Log("Finished replica exchange simulation");
		
		//print the outcome (vector after algorithm)...
		MyLogger.getLogger().Log("************ After Replica Exchange ************");
		MyLogger.getLogger().Log(cVec.toStringShort());
		MyLogger.getLogger().close();

	}
	
	/**
	 * this method is copied from the GA.java class (in package main)
	 */
	private static Configuration getConfiguration(String[] argv) {
		if (argv.length != 2){
			throw new RuntimeException("Cannot run GA \n Usage: java main.GA <config file name> <seed>");
		}
		Configuration config;
		System.out.print("Loading Configuration ...");
		String configFileName = argv[0];
		MyLogger.getLogger().Log("Configuration file name "+configFileName);
		int seed;
		try {
			seed = parseInt(argv[1]);
		} catch (NumberFormatException ex)   { throw new RuntimeException("Cannot parse "+argv[1]+" as a seed.");   }

		try {
			config = new Configuration(configFileName, seed);
		}  catch (IOException ex1) {throw new RuntimeException("Failed to find, open or read configuration file: "+configFileName+"\n"+ex1);}

		MyLogger.getLogger().Log("OK!");
		return config;
	}

}
